package top.kaoshanji.p2ch23a;

import net.sf.cglib.proxy.Enhancer;

/**
 * 代码清单-p2ch23a-584页a：cglib动态代理示例
 * author kaoshanji
 * ^_^
 * create time 2025/3/3 14:39
 */
public class RealServiceCGLibDemo {

    public static void main(String[] args) {
        RealServiceCGLibDemo demo = new RealServiceCGLibDemo();

        // 此对象是通过 cglib 创建出来的，被加了料
        // proxyService 是个代理对象
        RealServiceCGLib proxyService = demo.getProxy(RealServiceCGLib.class);

        proxyService.sayHello();

    }

    @SuppressWarnings("unchecked")
    private <T> T getProxy(Class<T> cls) {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(cls); // 被代理的类

        // 故事开始的地方
        enhancer.setCallback(new RealServiceCGLibInterceptor()); // 为被代理的类添加自定义逻辑的类

        return (T) enhancer.create();
    }



}
